#include "qcomboitemlistwidget.h"
#include "ui_qcomboitemlistwidget.h"

#include "../qsoftcore.h"
#include "../qfilemanager.h"
#include "../qtoolbarbutton.h"

QComboItemListWidget::QComboItemListWidget(QTreeWidgetItem *item, QTreeWidget *widget, QWidget *parent):
    QWidget(parent),
    ui(new Ui::QComboItemListWidget),
    m_tree_widget(widget),
    m_tree_item(item)
{
    ui->setupUi(this);

    QAction* ac;

    ac=new QAction(QIcon(":/images/up.png"),tr("Up"),this);
    m_up_button=new QToolBarButton(ac);
    connect(ac,SIGNAL(triggered()),this,SIGNAL(up()));

    ac=new QAction(QIcon(":/images/down.png"),tr("Down"),this);
    m_down_button=new QToolBarButton(ac);
    connect(ac,SIGNAL(triggered()),this,SIGNAL(down()));

    ac=new QAction(QIcon(":/images/remove.png"),tr("Delete"),this);
    m_delete_button=new QToolBarButton(ac);
    connect(ac,SIGNAL(triggered()),this,SIGNAL(remove()));


    ac=new QAction(QIcon(":/images/edit.png"),tr("Edit"),this);
    m_edit_button=new QToolBarButton(ac);
    connect(ac,SIGNAL(triggered()),this,SIGNAL(edit()));

    ui->horizontalLayout->addWidget(m_edit_button);
    ui->horizontalLayout->addWidget(m_up_button);
    ui->horizontalLayout->addWidget(m_down_button);
    ui->horizontalLayout->addWidget(m_delete_button);

    m_up_button->setVisible(false);
    m_down_button->setVisible(false);
    m_delete_button->setVisible(false);
    m_edit_button->setVisible(false);
}

QComboItemListWidget::~QComboItemListWidget()
{
    delete ui;
}

void QComboItemListWidget::set_text(const QString &text)
{
    ui->label_2->setText(text);
    setToolTip(text);
}

void QComboItemListWidget::set_icon(const QString &icon)
{
    QPixmap pix(":/"+icon);
    pix=pix.scaled(16,16,Qt::KeepAspectRatio,Qt::SmoothTransformation);
    ui->label->setPixmap(pix);
}

void QComboItemListWidget::enterEvent(QEvent *)
{
    int count=m_tree_widget->topLevelItemCount();
    int index=m_tree_widget->indexOfTopLevelItem(m_tree_item);

    m_up_button->setVisible(index>0);
    m_down_button->setVisible(index<count-1);
    m_delete_button->setVisible(true);
    m_edit_button->setVisible(true);
}

void QComboItemListWidget::leaveEvent(QEvent *)
{
    m_up_button->setVisible(false);
    m_down_button->setVisible(false);
    m_delete_button->setVisible(false);
    m_edit_button->setVisible(false);
}

QTreeWidgetItem* QComboItemListWidget::get_item()
{
    return m_tree_item;
}
